<?php

/**
 * @filesource Communicates with the Student control file to know which student
 * data to get and set. It is the only file that has the SQL queries and MySQL
 * connections
 *
 * @copyright 2011 by Dan, Steven, Brice, Mike, and Aaron.  This program is part
 * of Internship 2 Career, which is free software. It comes with absolutely no
 * warranty. You can redistribute or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation (For
 * more information, see <http://www.gnu.org/licenses/).
 *
 * @version February 8, 2011
 *
 * @author Brice Rader and Aaron Jarzombeck
 */

/**
 * This is the database file.
 * It controls the category file to list all categories and
 * to add or delete a category into the database.
 * It is the only file that has the SQL queries and MySQL connections
 */
class dbCategory {

   static function createTable() {
      //create table category
      $query = "
        create table category(
            categoryID      int             NOT NULL AUTO_INCREMENT,
            categoryName    varchar(45)     NOT NULL,
            constraint PK_Category PRIMARY KEY(categoryID)
            );
";
      connectToDatabase();
      $result = mysql_query($query);
      mysql_close();
      if ($result == false) {
         echo mysql_error();
         return false;
      }
      //mysql_close();
      //Previous query was successful
      return true;
   }

   /**
    * Gets all of the categories in the database
    * @return categories
    */
   function getAllCategories() {
      $query = "SELECT * FROM category";
      connectToDatabase();
      $result = mysql_query($query);

      if ($result == false) {
         echo mysql_error();
         mysql_close();
         return false;
      }

      //Query was successful.
      return $result;
   }

   /**
    * Add's a category to the database
    * @param category $c
	* @param category $id
    * @return new category added to database
    */
   static function addCategory($c, &$id) {
      if (!$c instanceof Category) {
         echo ("Invalid arguement insertInto ( ) function<br />");
         return false;
      }
      //Compose the INSERT INTO query srting. Primary keyvalue is missing.
      //Database server automatically generates a unique ID for the primary
      //key.
      $query = sprintf("INSERT INTO category VALUES (null, '%s')", $c->getCategoryName()
      );
      //printf("->Query: %s <br />", $query);
      connectToDatabase();
      $result = mysql_query($query);
      $id = mysql_insert_id();
      mysql_close();
      if ($result == false) {
         echo mysql_error();
         return false;
      }
      //query was successful.
      return true;
   }

   /**
    * Checks category ID
    * @param contact $c
    * @return checks to see what contacts are in the database
    */
   function checkCategoryID($c) {
      if (!($c instanceof Category)) {
         return false;
      }
      $categoryID = $c->getCategoryID();
      connectToDatabase();
      if (mysql_num_rows(mysql_query("SELECT * FROM category WHERE
                categoryID=$categoryID")) == false) {
         mysql_close();
         //nothing existsmysql_num_rows
         return false;
      } else {
         return true;
      }
   }

   /**
    * Get category ID from the database
    * @param $categoryID
    * @return ID
    */
   function getCategoryID($c) {
      $result = mysql_query("SELECT categoryID from Category WHERE
            categoryID='$c'");
      $row = mysql_fetch_row($result);
      return $row['0'];
   }

   /**
    * Get category name from the database
    * @param $CategoryName
    * @return Category
    */
   function getCategoryName($c) {
      $query = "SELECT categoryName FROM category WHERE categoryID='$c'";
      connectToDatabase();
      $result = mysql_query($query);
      mysql_close();
      if ($result == false) {
         echo mysql_error();
         return false;
      }
      //Query was successful.
      return $result;
   }

   /**
    * Delete's category from database
    * @param contact $c
    * @return categories in database 
    */
   function deleteCategoryID($c) {
      if (!($c instanceof Category)) {
         echo ("Invalid argument deleteFrom( ) function<br />");
         return false;
      }

      //Compose the DELETE FROM query string.
      $query = sprintf("DELETE FROM category WHERE categoryID = %d", $c->getCategoryID()
      );
      connectToDatabase();
      $result = mysql_query($query);
      mysql_close();
      if ($result == false) {
         echo mysql_error();
         return false;
      }

      //Query was successful.
      return true;
   }

   /**
   * @param contact $id
   */
   public static function getAllByID($id, &$output) {
      $query = sprintf("SELECT categoryName FROM category WHERE 
            categoryID=%d", $id);
      //printf("->Query: %s<br />", $query);
      connectToDatabase();
      $result = mysql_query($query);
      mysql_close();

      if ($result == false) {
         $output = sprintf("%s", mysql_error());
         return false;
      }
      //process result and print out warning or attribute values
      return dbCategory::checkForOneInstance($result, $id, $output);
   }

   /**
   * Checks for 1 instance
   * @param $result
   * @param contact $id
   */
   private static function checkForOneInstance($result, $id, &$output) {
      //Local variables are initialized
      $errorStatus = false;

      $howMany = mysql_num_rows($result);
      //printf("-->Found %d instance(s) for categoryID=%d.<br />, $howMany, $id);
      switch ($howMany) {
         case 0:
            $output = sprintf("No Category instance with ID=%d was found.", $id);
            $errorStatus = true;
            break;
         case 1:
            //mysql_fetch() DOES NOT return primary key values
            $output = mysql_fetch_assoc($result);
            //printf("->output: %s<br />", $output['categoryName']);
            $errorStatus = false;
            break;
         default:
            $output = sprintf("There are multiple Category instances with
                                   ID=%d.", $id);
            $errorStatus = true;
            break;
      }
      return $errorStatus;
   }

}

?>
